/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.util;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * @author bichtram
 */
public class MyHelperConnection {
    
    private final String conString = "jdbc:mysql://localhost/flowershop";
    
    public  Connection connection;
    
    public void openConnection (){
        try {
             Driver driver = new org.gjt.mm.mysql.Driver();
             DriverManager.registerDriver(driver);
             Properties info = new Properties();
             info.setProperty("characterEncoding", "utf8");
             info.setProperty("user", "root");
             info.setProperty("password", "");
             connection = DriverManager.getConnection(conString,info);
        }catch(SQLException ex){
            System.out.println(ex);
        }
    }
    
    public void closeConnection(){
        try{
        connection.close();
        }catch(SQLException ex){
            System.out.println(ex);
        }
    }
    
    public ResultSet excuteSelectQuery(String sql,String result){
        ResultSet rs;
        try{
            //OpenConnection();
            Statement statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            if(rs.next()==false){
                result="Không Tìm Thấy Dữ Liệu";
            }
            //CloseConnection();
            
            return rs;
        }catch(SQLException ex){ 
            result+=ex.getMessage();
        }
        return null;
    }
    
    //IDU: Insert - Delete - Update: return 0: co loi, return #0: thanh cong
    public int excuteIDUQuery(String sql){
        try{
            Statement statement = connection.createStatement();
            int n = statement.executeUpdate(sql);
            return n;
        }
        catch(SQLException ex){ 
        }
        return 0;
    }
}
